Walkthrough 11-4: Transform to and from XML with repeated elements
In this walkthrough, you continue to work with the JSON data for multiple flights posted to the flow. You will:
· Transform a JSON array of objects to XML.
· Replace sample data associated with a transformation.
· Transform XML with repeated elements to different data types.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Transform the JSON array of objects to XML
1. Return to the Transform Message properties view for the transformation in postMultipleFlights.
2. Change the expression to map each item in the input array to an XML element.
flights: {(payload map (object,index) -> {
'flight$(index)': object
}
)}
3. Look at the preview; the JSON should be transformed to XML successfully.
4. Modify the expression so the flights object has a single property called flight.
Change the input metadata to XML
5. In the input section, right-click the payload and select Clear Metadata.
6. Click the Define metadata link.
7. In the Select metadata type dialog box, select flights_xml and click Select.
8. In the Transform Message Properties view, you should now see new metadata for the input.
Preview sample data and sample output
9. In the input section, click the x on the payload tab to close it.
10. In the Close Sample Data dialog box, click Close tab and Keep file.
11. Right-click the payload and select Edit Sample Data to get sample input data.
12. Look at the XML sample payload.
13. Look at the sample_data folder in src/test/resources.
14. Review the transformation expression; you should see an issue.
15. Look at the error.
Transform XML with repeated elements to a different XML structure
16. Look at the payload metadata in the input section.
17. Change the DataWeave expression so that the map is iterating over payload..return.
18. Change the DataWeave expression so that the map is iterating over the repeated return elements of the input.
19. Change the DataWeave expression to return flight elements with dest and price elements that map to the corresponding destination and price input values.
Note: If you want to test the application with Advanced REST Client, be sure to change the content-type header to application/xml and replace the request body with XML from the flights-example.xml file.
Change the expression to output different data types
20. Change the output type from application/xml to application/dw.
21. Change the output type to application/json.
22. Change the output type to application/java.
23. In the DataWeave expression, remove the {( )} around the map expression.
24. In the DataWeave expression, remove the flight property; you should get an ArrayList with five LinkedHashMaps.